package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import model.ComplaintReportModel;
import view.ArchiveReportGUI;
import client.CliMessage;

/**
 * The Class ArchiveReportController that extends AbstractController implements IValidateInput.
 * @author yudku
 */
public class ArchiveReportController extends AbstractController implements IValidateInput
{	
	
	/** The Archive report view. */
	private ArchiveReportGUI  ArchiveReportView;	
	/** The Comp report model. */
	private ComplaintReportModel CompReportModel;
	
	/**
	 * This is ArchiveReportController controller constructor.
	 *
	 * @param ArchiveReportView the archive report view
	 * @param CompReportModel the comp report model
	 */
	
	public ArchiveReportController( ArchiveReportGUI ArchiveReportView, ComplaintReportModel CompReportModel) 
	{
		super(ArchiveReportView,CompReportModel);
		this.ArchiveReportView =  ArchiveReportView;
		this.CompReportModel = CompReportModel;
		
		String searchQuery = "search"	+ "SELECT * "
				+ "FROM photos";	
		
        sendToServer(searchQuery);

	}	
	
	public void setButtons(ComplaintReportModel CompReportModel)
	{		
		 ArchiveReportView.addReplyActionListener(new ReplyListener(CompReportModel));
	}

	@Override
	public boolean validateInput() 
	{
		return true;		
	}
	
	

	/**
	 * Inner class that handles when Reply Button Pressed, implements Action listener.
	 *
	 * @author Tal & Yuda
	 */
	
	class ReplyListener implements ActionListener
	{
		
		/** The Comp report model. */
		private ComplaintReportModel CompReportModel;
		
		
		/**
		 * Instantiates a new reply listener.
		 *
		 * @param CompReportModel the comp report model
		 */
		public ReplyListener(ComplaintReportModel CompReportModel)
		{
			this.CompReportModel = CompReportModel;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if(!validateInput())
				return;
	  
			int size = CompReportModel.getcomplaintNum();;
			int newphoto =  CompReportModel.getmonthleycomp();
			String det = CompReportModel.getdet();
			System.out.println (det);
			
			//insert reply to complaint id and change status in db
           String insertQuery = "adu"	+"INSERT INTO archive_report "
										+"(archivesize ,newphoto, photodet) VALUES "
										+"("+ size +","+ newphoto +",'"+ det +"')";
           
            sendToServer(insertQuery); 
            ArchiveReportView.clickBackButton();
		}
	}

	@Override
	public void handleDBRequestSuccess()
	{
		 ArchiveReportView.displayWrnMessage("Reply Succesfully Enterd");		
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult)
	{	
		int newPhoto = 0;
		ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
		
		Calendar cals = Calendar.getInstance();		
		int currMonth = cals.get(Calendar.MONTH);
		currMonth++;
		int currYear = cals.get(Calendar.YEAR);		
		int ArchiveSize = 0;
		int Size=srs.size();
		String photoData = "";
		
		for(int i = 0 ; i < Size  ; i++)
		{
		   Map<String, String> archiveTupple  = srs.get(i);	
		   
		   int DBmonth = (Integer.parseInt((archiveTupple.get("DATE")).substring(5, 7)));
		   int DByear = (Integer.parseInt((archiveTupple.get("DATE")).substring(0, 4)));
		   int DBday = (Integer.parseInt((archiveTupple.get("DATE")).substring(8, 10)));
		   
		   System.out.println ("year: " + DByear + " month: " + DBmonth + " day: " + DBday);
					
		   if ((currMonth == DBmonth) && (currYear == DByear) && ( DBday >= 1) && ( DBday <=31))
		   {
			   if ( (archiveTupple.get("LOCATION")) != null)
				   photoData = photoData + "Location: " + (archiveTupple.get("LOCATION")) + " Time stamp: " + (archiveTupple.get("TIMESTAMP")).substring(0,16) + " Path: " + (archiveTupple.get("LINKTOFILE")) + "\n";
			   else
			   {
				   newPhoto = newPhoto + Integer.parseInt((archiveTupple.get("NEWPHOTO")));
				   if (i == (Size - 1))
					   ArchiveSize = Integer.parseInt((archiveTupple.get("ARCHIVESIZE")));
			   }
		   }
		}	
		
		   
		///set fields GUi
		 if (photoData == "")
		 {
			   CompReportModel.setcomplaintNum(ArchiveSize);
			   CompReportModel.setmonthleycomp(newPhoto);
			   ArchiveReportView.SetGenLabel(Integer.toString(CompReportModel.getcomplaintNum()),   ArchiveReportView.getlabel1_2());
			   ArchiveReportView.SetGenLabel( Integer.toString(CompReportModel.getmonthleycomp()),   ArchiveReportView.getlabel3_2());
		 }
		 else
		 {
			   CompReportModel.setdet(photoData);
			   ArchiveReportView.setPhotoDet(photoData);
				setButtons(CompReportModel);
		 }

		//display main window
		 ArchiveReportView.setVisible(true);						
	}

	@Override
	public void handleNullSearchResult() 
	{
		 ArchiveReportView.displayWrnMessage("DATA DOES NOT EXIST!");
		
	}

}
